package util;

import javax.swing.*;
import java.awt.*;

/**
 * @description:
 * @author: ziger
 * @time: 2022/9/9 16:24
 */
public class FrameUtil {
    /**
     * Centers window on desktop.
     * <p>
     *
     * @param aFrame the Frame to center
     */
    public static void center(JFrame aFrame) {
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        final Point centerPoint = ge.getCenterPoint();
        final Rectangle bounds = ge.getMaximumWindowBounds();
        final int w = Math.min(aFrame.getWidth(), bounds.width);
        final int h = Math.min(aFrame.getHeight(), bounds.height);
        final int x = centerPoint.x - (w / 2);
        final int y = centerPoint.y - (h / 2);
        aFrame.setBounds(x, y, w, h);
        if ((w == bounds.width) && (h == bounds.height)) {
            aFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
        }
        aFrame.validate();
    }
}
